package com.zwStorage.Util;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.zwStorage.Model.AAA;
import com.zwStorage.Model.other;
import net.minidev.json.JSONObject;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * zengwei
 * 日期：2020/5/27 20:30
 */
public class Util {

    public static void main(String[] a){

        AAA aaa=new AAA();
        aaa.setName("联想电脑S400");
        aaa.setBrand("联想");
        aaa.setType("台式机");
        aaa.setSpecifications("asdasd");
        aaa.setNumber(2);
        aaa.setPrice(2*5000);
        aaa.setTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
        aaa.setOutBound(false);
        addAAA(aaa);
       // System.out.println(getOhter());
    }
    //获取项目的根路径
    public static String getProjectPaht(){
        String relativelyPath=System.getProperty("user.dir");
        return relativelyPath;
    }

    /**文件读取**/
    public static String readJsonFile(String fileName) {
        String jsonStr = "";
        try {
            File jsonFile = new File(fileName);
            FileReader fileReader = new FileReader(jsonFile);

            Reader reader = new InputStreamReader(new FileInputStream(jsonFile),"utf-8");
            int ch = 0;
            StringBuffer sb = new StringBuffer();
            while ((ch = reader.read()) != -1) {
                sb.append((char) ch);
            }
            fileReader.close();
            reader.close();
            jsonStr = sb.toString();
            return jsonStr;
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    /**文件覆盖**/
    public static boolean addFile(String string,String path) {
        PrintStream stream=null;
        try {

            stream=new PrintStream(path);//写入的文件path
            stream.print(string);//写入的字符串
            return true;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        return false;
    }



    //获取其他数据
    public static other getOhter(){
        String path = getProjectPaht()+"\\z_other.json";
        String jsonString = readJsonFile(path);
        Gson gson = new Gson();
        other other = gson.fromJson(jsonString, other.class);
        return other;
    }

     //获取入库信息
    public static List<AAA> getAAA(){
        String path = getProjectPaht()+"\\z_storage.json";
        String jsonString = readJsonFile(path);
        Gson gson = new Gson();
        List<AAA> persons =gson.fromJson(jsonString, new TypeToken<List<AAA>>() {}.getType());
        return persons;
    }

    //添加入库信息
    public static boolean addAAA(AAA aaa){
        try {
            String path = getProjectPaht()+"\\z_storage.json";
            List<AAA> aaas=getAAA();
            aaa.setId(aaas.size()+1);
            aaas.add(aaa);
            return  addFile(new Gson().toJson(aaas),path);
        }catch (Exception e){
            return false;
        }
    }



    public static void outbound(int i){
        List<AAA> aaas=getAAA();
        for(AAA a:aaas){
            if (a.getId()==i){
                a.setOutBound(true);
            }
        }
        String path = getProjectPaht()+"\\z_storage.json";
        addFile(new Gson().toJson(aaas),path);
    }

}
